<?php
header("Content-type: text/html; charset=utf-8");
set_time_limit(0);
include_once dirname(dirname(dirname(__FILE__))) . '/mgtv_init.php';
include_once dirname(dirname(__FILE__)) . '/init.php';
include_once dirname(dirname(dirname(__FILE__))) . '/import/import.class.php';

$db_r = nn_get_db(NL_DB_READ);
$db_w = nn_get_db(NL_DB_WRITE);
$url = IMPROT_EPG_URL;
$log = IMPROT_EPG_LOG;
$sql = "select task.nns_ref_id,  epg.nns_video_id,task.nns_status,task.nns_action from nns_mgtvbk_c2_task as task 
left join  nns_mgtvbk_import_epg as epg
 on (task.nns_type=epg.nns_video_type and epg.nns_video_id=task.nns_ref_id)
 where task.nns_org_id='jllt' and task.nns_type='vod' and task.nns_status=0 and task.nns_org_id='jllt' and epg.nns_video_id is NULL limit 200";

$list = nl_db_get_all($sql, $db_r);
if (count($list) > 0) {
	foreach ($list as $value) {
		$action = 'add';//$value['nns_action'];
		$import_obj = new import($url, $log);
		$sql = "select * from nns_vod where nns_id='{$value['nns_ref_id']}'";
		$info = nl_db_get_one($sql, $db_r);
		
		/*$sql = "select count(*) as tmp from nns_vod_media as med left join nns_mgtvbk_c2_task as task
		on (med.nns_id=task.nns_ref_id) where task.nns_org_id='jllt' and  med.nns_vod_id='{$value['nns_ref_id']}' and task.nns_type='media' 
		and ((task.nns_action='add' and task.nns_status=0) or task.nns_action='modify')";*/
		
		
		$sql = "select count(*) from nns_mgtvbk_c2_task  as  task  left join nns_vod_media  as media on ( media.nns_id = task.nns_ref_id 
 and media.nns_vod_id='{$value['nns_ref_id']}')
where  task.nns_type='media' and  task.nns_org_id='jllt' and  task.nns_status=0";
		
		$count = nl_db_get_col($sql, $db_r);
		
		
		
		
		$xml = '<video assets_id="' . $info['nns_id'] . '" ';
		$sql = "select * from nns_depot where nns_id='{$info['nns_depot_id']}'";
		$depot = nl_db_get_one($sql, $db_r);

		$nns_asset_import_id = $info['nns_asset_import_id'];
		$sql = "SELECT * FROM `nns_vod_ex` where nns_vod_id='{$nns_asset_import_id}'";
		$vod_ex = nl_db_get_all($sql, $db_r);

		$category_name = '';
		$dom = new DOMDocument('1.0', 'utf-8');
		$depot_info = $depot['nns_category'];
		$nns_category_id = $info['nns_category_id'];
		$dom -> loadXML($depot_info);
		$categorys = $dom -> getElementsByTagName('category');
		foreach ($categorys as $category) {
			if ($category -> getAttribute('id') == (int)$nns_category_id) {
				$category_name = $category -> getAttribute('name');
				break;
			}
		}
		$xml .= 'assets_category="' . $category_name . '" video_type="0" ';
		$xml .= 'name="' . $info['nns_name'] . '" ';
		$xml .= 'sreach_key="' . str_replace("/", " ", $info['nns_keyword']) . '" ';
		$xml .= 'view_type="' . $info['nns_view_type'] . '" ';
		$xml .= 'director="' . str_replace("/", " ", $info['nns_director']) . '" ';
		$xml .= 'player="' . str_replace("/", " ", $info['nns_actor']) . '" ';
		$xml .= 'tag="' . str_replace("/", " ", $info['nns_kind']) . '" ';
		$xml .= 'region="' . str_replace("/", " ", $info['nns_area']) . '" ';
		$xml .= 'release_time="' . $info['nns_show_time'] . '" ';
		$xml .= 'totalnum="' . $info['nns_all_index'] . '" ';
		foreach ($vod_ex as $value) {
			$xml .= $value['nns_key'] . '="' . $value['nns_value'] . '" ';
		}
		$xml .= 'smallpic="' . $info['nns_image2'] . '" ';
		$xml .= 'bigpic="' . $info['nns_image0'] . '" ';
		$xml .= 'intro="' . $info['nns_summary'] . '" ';
		$xml .= 'producer="' . str_replace("/", " ", $info['nns_producer']) . '" ';
		$xml .= 'play_role="' . $info['nns_play_role'] . '" ';
		$xml .= 'subordinate_name="' . str_replace("/", " ", $info['nns_alias_name']) . '" ';
		$xml .= 'english_name="' . str_replace("/", " ", $info['nns_eng_name']) . '" ';
		$xml .= 'language="' . str_replace("/", " ", $info['nns_language']) . '" ';
		$xml .= 'caption_language="' . str_replace("/", " ", $info['nns_text_lang']) . '" ';
		$xml .= 'copyright_range="' . str_replace("/", " ", $info['nns_remark']) . '" ';
		$xml .= 'copyright="' . str_replace("/", " ", $info['nns_copyright']) . '" ';
		$xml .= 'total_clicks="' . $info['nns_play_count'] . '" ';
		$xml .= 'day_clicks="0" ';
		$xml .= '></video>';
		//媒资注入
		if ($action == 'destroy') {
			$result = $import_obj -> delete_assets($info['nns_id']);
			$xml = "";
		} else {
			if($count>0){
				$result = $import_obj -> import_assets($xml);
			}else{
				$result = json_encode(array('ret'=>1,'reason'=>'该片片源没有注入成功','data'=>''));
			}			
		}
		$re = json_decode($result, true);
		//print_r($re);
		if ($re['ret'] == '0') {
			$data = array('nns_id' => np_guid_rand('vod'), 'nns_video_id' => $info['nns_id'], 'nns_video_type' => 'vod', 'nns_org_id' => SP_ORG_ID, );
			nl_db_insert($db_w, 'nns_mgtvbk_import_epg', $data);
		}
		$log_data = array(
			'nns_id' => np_guid_rand('vod'),
			'nns_video_id' => $info['nns_id'],
			'nns_video_type' => 'vod', 
			'nns_org_id' => SP_ORG_ID,
			'nns_create_time'=>date('Y-m-d H:i:s'),
			'nns_action'=>$action,
			'nns_status'=>$re['ret'],
			'nns_reason'=>$re['reason'],
			'nns_data'=>$xml,
		);
		nl_db_insert($db_w, 'nns_mgtvbk_import_epg_log', $log_data);
	}
}
$sql = "select task.nns_ref_id,task.nns_src_id,  epg.nns_video_id,task.nns_status,task.nns_action from nns_mgtvbk_c2_task as task 
left join  nns_mgtvbk_import_epg as epg
 on (task.nns_type=epg.nns_video_type and epg.nns_video_id=task.nns_ref_id)
 where task.nns_org_id='jllt' and  task.nns_type='index' and task.nns_org_id='jllt' and task.nns_status=0 and epg.nns_video_id is NULL limit 200";
$list = nl_db_get_all($sql, $db_r);
if (count($list) > 0) {
	foreach ($list as $value) {
		$action = 'add';//$value['nns_action'];
		$import_obj = new import($url, $log);
		$sql = "select * from nns_vod_index where nns_id='{$value['nns_ref_id']}'";
		
		$info = nl_db_get_one($sql, $db_r);
		/*$sql = "select count(*) as tmp from nns_vod_media as med left join nns_mgtvbk_c2_task as task
		on (med.nns_id=task.nns_ref_id) where task.nns_org_id='jllt' and  med.nns_vod_index_id='{$value['nns_ref_id']}' and task.nns_type='media' 
		and ((task.nns_action='add' and task.nns_status=0) or task.nns_action='modify')";*/
		
		$sql = "select count(*) from nns_mgtvbk_c2_task  as  task  left join nns_vod_media  as media on ( media.nns_id = task.nns_ref_id 
 and media.nns_vod_index_id='{$value['nns_ref_id']}')
where  task.nns_type='media' and  task.nns_org_id='jllt' and  task.nns_status=0";
		
		$count = nl_db_get_col($sql, $db_r);		
		$xml = '<index clip_id="' . $info['nns_id'] . '" ';
		$xml .= 'name="' . $info['nns_name'] . '" ';
		$xml .= 'index="' . $info['nns_index'] . '" ';
		$xml .= 'time_len="' . $info['nns_time_len'] . '" ';
		$xml .= 'summary="' . $info['nns_summary'] . '" ';
		$xml .= 'pic="' . $info['nns_image'] . '" ';
		$xml .= 'director="' . str_replace("/", " ", $info['nns_director']) . '" ';
		$xml .= 'player="' . str_replace("/", " ", $info['nns_actor']) . '" ';
		$xml .= 'total_clicks="' . $info['nns_play_count'] . '" ';
		$xml .= 'release_time="' . $info['nns_create_time'] . '" ';
		$xml .= 'update_time="' . $info['nns_modify_time'] . '" ';
		$xml .= 'month_clicks="" ';
		$xml .= 'week_clicks="" ';
		$xml .= 'day_clicks="" ';
		$xml .= '></index>';
		if ($action == 'destroy') {
			$result = $import_obj -> delete_video_clip($info['nns_id']);
			$xml="";
		} else {
			if($count>0){
				$result = $import_obj -> import_video_clip($info['nns_vod_id'], $info['nns_id'], $xml);
			}else{
				$result = json_encode(array('ret'=>1,'reason'=>'该分集片源没有注入成功','data'=>''));
			}
		}
		$re = json_decode($result, true);
		
		print_r($re);
		if ($re['ret'] == '0') {
			$data = array('nns_id' => np_guid_rand('index'), 'nns_video_id' => $info['nns_id'], 'nns_video_type' => 'index', 'nns_org_id' => SP_ORG_ID, );
			nl_db_insert($db_w, 'nns_mgtvbk_import_epg', $data);
		}
		$log_data = array(
			'nns_id' => np_guid_rand('index'),
			'nns_video_id' => $info['nns_id'],
			'nns_video_type' => 'index', 
			'nns_org_id' => SP_ORG_ID,
			'nns_create_time'=>date('Y-m-d H:i:s'),
			'nns_action'=>$action,
			'nns_status'=>$re['ret'],
			'nns_reason'=>$re['reason'],
			'nns_data'=>$xml,
		);
		nl_db_insert($db_w, 'nns_mgtvbk_import_epg_log', $log_data);
		
		unset($info);
	}
}
$sql = "select task.nns_ref_id,  epg.nns_video_id,task.nns_status,task.nns_action from nns_mgtvbk_c2_task as task 
left join  nns_mgtvbk_import_epg as epg
 on (task.nns_type=epg.nns_video_type and epg.nns_video_id=task.nns_ref_id)
 where task.nns_org_id='jllt' and  task.nns_type='media' and task.nns_status=0 and epg.nns_video_id is NULL limit 200";
$list = nl_db_get_all($sql, $db_r);
if (count($list) > 0) {
	foreach ($list as $value) {
		$action = 'add';//$value['nns_action'];
		$import_obj = new import($url, $log);
		$sql = "select * from nns_vod_media where nns_id='{$value['nns_ref_id']}'";
		$info = nl_db_get_one($sql, $db_r);
		
		//查询c2状态
		$sql = "select * from nns_mgtvbk_c2_task where nns_org_id='".SP_ORG_ID."' and nns_ref_id='".$info['nns_vod_index_id']."' and nns_src_id='".$info['nns_vod_id']."'";
		$c2_task = nl_db_get_one($sql, $db_r);
		
		$status = 0;
		
		if(($c2_task['nns_action']=='add'&&$c2_task['nns_status']==0)||$c2_task['nns_action']=='modify'||$c2_task['nns_action']=='destroy'){
			$status = 1;
		}
		
		$xml = '<media file_id="' . $info['nns_id'] . '" file_type="' . $info['nns_filetype'] . '" ';
		$xml .= 'file_path="' . $info['nns_url'] . '" file_definition="' . $info['nns_mode'] . '" ';
		$xml .= 'file_resolution="" file_size="" file_bit_rate="' . $info['nns_kbps'] . '" file_desc="" ';
		if ($info['nns_dimensions'] == '0') {
			$xml .= 'file_3d="0" file_3d_type=""></media>';
		} elseif ($info['nns_dimensions'] == '100001') {
			$xml .= 'file_3d="1" file_3d_type="0"></media>';
		} elseif ($info['nns_dimensions'] == '100002') {
			$xml .= 'file_3d="1" file_3d_type="1"></media>';
		}
		if ($action == 'destroy') {
			$result = $import_obj -> delete_video_file($info['nns_id']);
			$xml="";
		} else {
			if($status>0){
				$result = $import_obj -> import_video_file($info['nns_vod_id'], $info['nns_vod_index_id'], $info['nns_id'], $xml);
			}else{
				$result = json_encode(array('ret'=>1,'reason'=>'该片源所属分集没有注入成功','data'=>''));
			}
		}
		$re = json_decode($result, true);
		//print_r($re);
		if ($re['ret'] == '0') {
			$data = array('nns_id' => np_guid_rand('media'), 'nns_video_id' => $info['nns_id'], 'nns_video_type' => 'media', 'nns_org_id' => SP_ORG_ID, );
			nl_db_insert($db_w, 'nns_mgtvbk_import_epg', $data);
		}		
		$log_data = array(
			'nns_id' => np_guid_rand('media'),
			'nns_video_id' => $info['nns_id'],
			'nns_video_type' => 'media', 
			'nns_org_id' => SP_ORG_ID,
			'nns_create_time'=>date('Y-m-d H:i:s'),
			'nns_action'=>$action,
			'nns_status'=>$re['ret'],
			'nns_reason'=>$re['reason'],
			'nns_data'=>$xml,
		);
		nl_db_insert($db_w, 'nns_mgtvbk_import_epg_log', $log_data);

	}
}
?>